
Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			{name:'id',type:'string'},
			{name:'create_time',type:'string'},
			{name:'begin_time',type:'string'},
			{name:'end_time',type:'string'},
			{name:'state',type:'string'},
			{name:'course_hour',type:'string'},
			{name:'course_id',type:'string'},
			{name:'course__name',type:'string'},
			{name:'attend',type:'string'},
			{name:'study_type',type:'string'},
			{name:'remark',type:'string'},
			{name:'company_id',type:'string'},
			{name:'grade_id',type:'string'},
			{name:'grade__name',type:'string'},
			{name:'company_id',type:'string'},
			{name:'company__cn_name',type:'string'},
			{name:'student_id',type:'string'},
			{name:'student__name',type:'string'},
			{name:'teacher_id',type:'string'},
			{name:'teacher__name',type:'string'},
			{name:'student_manager_id',type:'string'},
			{name:'student_manager__name',type:'string'},
			{name:'auditor_id',type:'string'},
			{name:'auditor__name',type:'string'},
			{name:'szjy_id',type:'string'},
			{name:'szjy__name',type:'string'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/resourcecontrol/course_real_record_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: false
    });
    
    
    //校区combox
    var companyFormComboBox = Ext.create('Ext.form.field.ComboBox', {
		id : 'company_id',
		name : 'company_id',
		xtype : 'combobox',
		fieldLabel : '校区',
		emptyText : '请选择校区',
		editable : false,
		allowBlank : false,
		store : new Ext.data.Store( {
			singleton : true,
			proxy : {
				type : 'ajax',
				url : '/resourcecontrol/company_select/',
				actionMethods : 'post',
				reader : {
					type : 'json'
				}
			},
			fields : [ 'id', 'cn_name' ],
			autoLoad : true
		}),
		loadingText : '正在加载数据，请稍侯……',
		triggerAction : 'all',
		valueField : 'id',
		displayField : 'cn_name',
		listeners : {
			'select' : function(combo, records, opts) {
				var params = {
					company_id : Ext.getCmp('company_id').getValue()
				};
				Ext.apply(studentFormComboBox.getStore().proxy.extraParams,params);
				// studentComboBox.getStore().load();
				studentFormComboBox.enable();
			}
		}
	});
    // 员工-学管师combox
    var studentManagerComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id : 'student_manager_combobox_id',
    	name:'student_manager_id',
    	fieldLabel: '<b><font color=red>学管师</font></b>', 
    	emptyText: '参写课时/托管提成',
    	typeAhead:true,
        minChars:1,
//      labelWidth: 60,
//	    width : 150,
    	editable:true,
    	allowBlank : false,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
	    		type: 'ajax',
	    		url: '/employecontrol/employe_select30/',
	    		actionMethods:'post',
	    		reader: {
	    			type: 'json'
    			}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
    		'select': function(combo, records, opts) {	
   	    		
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
    //studentManagerComboBox.getStore().load({params:{ office_id:'30' }}) ;
    
    
    //员工-教师combox
    var teacherComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id : 'teacher_id',
    	name:'teacher_id',
    	fieldLabel: '<b><font color=red>教师</font></b>', 
    	emptyText: '',//参与课时/托管提成
    	typeAhead:true,
        minChars:1,
//    	labelWidth: 60,
//    	width : 150,
    	editable:true,
    	allowBlank : true,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
    		type: 'ajax',
    		url: '/employecontrol/employe_select29/',
    		actionMethods:'post',
    		reader: {
    		type: 'json'
    	}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
    		'select': function(combo, records, opts) {	
   	    		//alert("待增加学生查找") ;
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
     //员工-素质拓展师combox
    var szjyComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id : 'szjy_id',
    	name:'szjy_id',
    	fieldLabel: '<b><font color=red>素质拓展师</font></b>', 
    	emptyText: '',//参与课时
    	typeAhead:true,
        minChars:1,
//    	labelWidth: 60,
//    	width : 150,
    	editable:true,
    	allowBlank : true,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
    		type: 'ajax',
    		url: '/employecontrol/employe_select_all/',
    		actionMethods:'post',
    		reader: {
    		type: 'json'
    	}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
    		'select': function(combo, records, opts) {	
   	    		//alert("待增加学生查找") ;
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 

    	}
    });
    
  //年级combox
    var gradeComboBox= Ext.create('Ext.form.field.ComboBox', { 
	   name:'grade_id',
       fieldLabel: '年级', 
       emptyText: '请选择学生的年级',
       editable:false,
       allowBlank : false,
	   width: 245,
	   labelWidth:120,
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/customercontrol/grade_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name'
    }); 


  	//课程combox
    var courseComboBox= Ext.create('Ext.form.field.ComboBox', {
	   name:'course_id',
       fieldLabel: '课程/托管', 
       emptyText: '请选择课程/托管课程',
       editable:false,
       allowBlank : false,
//       labelWidth: 60,
//   	   width : 150,
       store: new Ext.data.Store({ 
           singleton : true, 
	   	   proxy: {
	   	        type: 'ajax',
	   	        url: '/customercontrol/course_select/',
	   	        actionMethods:'post',
	   	        reader: {
	   	            type: 'json'
	   	        }
	   	    },
           fields:['id', 'name'],
           autoLoad:true 
       }), 
       loadingText:'正在加载数据，请稍侯……', 
       triggerAction:'all', 
       valueField:'id', 
       displayField:'name',
       listeners: {
    		'select': function(combo, records, opts) {	
    			var courseHourTextField=Ext.getCmp('form').getForm().findField('course_hour');
   	    		if(this.getValue()>10){
   	    			Ext.getCmp('form').getForm().setValues({course_hour:'1'});
   	    			courseHourTextField.disable();
   	    		}else{
   	    			Ext.getCmp('form').getForm().setValues({course_hour:''});
   	    			courseHourTextField.enable();
   	    		}
    		}
    	}
    }); 
    
    //学生combox
    var studentFormComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id:'student_id',
    	name:'student_id',
    	emptyText: '请选择学生',
//    	labelWidth: 30,
//    	width : 120,
    	fieldLabel: '学生', 
    	typeAhead:true,
        minChars:1,
    	editable:true,
    	disabled:false,
    	allowBlank : false,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
    		type: 'ajax',
    		url: '/customercontrol/company_student_select/',
    		actionMethods:'post',
    		reader: {
    		type: 'json'
    	}
    	},
    	fields:['id', 'name'],
    	autoLoad:true 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
	    	'select': function(combo, records, opts) {	
	    		
	    	},
	    	'beforequery' : function(queryEvent,eOpts){
	    		var params = {
					company_id : Ext.getCmp('company_id').getValue()
				};
				Ext.apply(studentFormComboBox.getStore().proxy.extraParams,params);
				
	    		queryEvent.combo.getStore().load();
	    	} 
    	}
    	}); 
    
  //学生combox
    var studentComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id:'student_combobox_id',
    	name:'student_id',
    	emptyText: '请选择学生',
    	labelWidth: 30,
    	width : 120,
    	fieldLabel: '学生', 
    	typeAhead:true,
        minChars:1,
    	editable:true,
    	disabled:true,
    	store: new Ext.data.Store({ 
    		singleton : true, 
    		proxy: {
    		type: 'ajax',
    		url: '/customercontrol/company_student_select/',
    		actionMethods:'post',
    		reader: {
    		type: 'json'
    	}
    	},
    	fields:['id', 'name'],
    	autoLoad:false 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'name',
    	listeners: {
    		'select': function(combo, records, opts) {	
   	    		//alert("待增加学生查找") ;
    		},
    		'beforequery' : function(queryEvent,eOpts){
				queryEvent.combo.getStore().load();
			} 
    	}
    }); 
    
    //课时状态combox
    var recordStateComboBox= Ext.create('Ext.form.field.ComboBox', { 
    	id:'record_state_combobox_id',
    	name:'record_state_id',
    	emptyText: '请选择状态',
    	labelWidth: 60,
    	width : 180,
    	fieldLabel: '课时状态', 
    	editable:false,
    	store: new Ext.data.Store({ 
			fields:['id','value'], 
			data:[
			    {'id':'' ,'value':'全部'}, 
				{'id':'1','value':'未审核'},
				{'id':'2','value':'审核通过'},
				{'id':'3','value':'驳回'}
			],
	    	autoLoad:false 
    	}), 
    	loadingText:'正在加载数据，请稍侯……', 
    	triggerAction:'all', 
    	valueField:'id', 
    	displayField:'value'
    }); 
    
    
    
    
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        //renderTo: 'courserecord-tab',
        //selModel:Ext.create('Ext.selection.CheckboxModel'),
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
            {text: "ID", width: 100, dataIndex: 'id', sortable: false,hidden : true},
            {text: "记录时间", width: 80, dataIndex: 'create_time', sortable: false},
            {text: "校区", width: 80, dataIndex: 'company__cn_name', sortable: false},
            {text: "学生ID", width: 100, dataIndex: 'student_id', sortable: false,hidden : true},
            {text: "学生", width: 60, dataIndex: 'student__name', sortable: false},
            {text: "学管师ID", width: 60, dataIndex: 'student_manager_id', sortable: false,hidden : true},
            {text: "学管师", width: 60, dataIndex: 'student_manager__name', sortable: false},
            {text: "授课老师ID", width: 100, dataIndex: 'teacher_id', sortable: false,hidden : true},
            {text: "授课老师", width: 60, dataIndex: 'teacher__name', sortable: false},
            {text: "素质拓展师ID", width: 100, dataIndex: 'szjy_id', sortable: false,hidden : true},
            {text: "素质拓展师", width: 60, dataIndex: 'szjy__name', sortable: false},
            {text: "年级ID", width: 100, dataIndex: 'grade_id', sortable: false,hidden : true},
            {text: "年级", width: 50, dataIndex: 'grade__name', sortable: false},
            {text: "课程ID", width: 100, dataIndex: 'course_id', sortable: false,hidden : true},
            {text: "课程", width: 100, dataIndex: 'course__name', sortable: false},
            {text: "课时", width: 35, dataIndex: 'course_hour', sortable: false},
            {text: "学习方式", width: 60, dataIndex: 'study_type', sortable: false,
                renderer:function(v){
	            	if (v==1) 
	                	return '一对一'  
	                else if(v==2)
	                    return '一对二' 
	                else if(v==3)
	                    return '一对三'    
	                else if(v==4)
	                	return '一对四'    
	    			else if(v==5)
	        			return '一对五'
	        		else if(v==6)
	        			return '户外'
	            }
            },
            {text: "上课时间", width: 60, dataIndex: 'begin_time', sortable: false},
            {text: "下课时间", width: 60, dataIndex: 'end_time', sortable: false},
            {text: "出席状态", width: 60, dataIndex: 'attend', sortable: false,
                renderer:function(v){
            	if (v==1) 
                	return '迟到'  
    			else if(v==2)
    				return '请假'
				else if(v==3)
					return '旷课'
    			else
        			return '正常'
            	}},
            {text: "校区ID", width: 100, dataIndex: 'company_id', sortable: false,hidden : true},
            {text: "审批状态", width: 80, dataIndex: 'state', sortable: false,
                renderer:function(v){
            	if (v==1) 
                	return '未审核'  
    			else if(v==2)
    				return '审核通过'
    			else
        			return '驳回'
            	}},
            {text: "备注", width: 100, dataIndex: 'remark', sortable: false},
            {text: "审批人ID", width: 100, dataIndex: 'auditor_id', sortable: false,hidden : true},
            {text: "审批人", width: 100, dataIndex: 'auditor__name', sortable: false}
        ],
        //title: '课时记录',
        layout : 'fit',
        //frame:true,
        loadMask:true,
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	form.getForm().reset();
        			var selected = grid.getSelectionModel().getSelection();
					form.loadRecord(selected[0]);
        			win.show();
                }
    		}
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		xtype: 'combobox',
        		labelWidth: 30,
        		width : 120,
        		id:'company_combox_id',
            	name:'company',
               	fieldLabel: '校区',
               	emptyText: '请选择校区',
               	editable:false,
               	store: new Ext.data.Store({
                   	singleton : true, 
        	   	    proxy: {
        	   	        type: 'ajax',
        	   	        url: '/resourcecontrol/company_select/',
        	   	        actionMethods:'post',
        	   	        reader: {
        	   	            type: 'json'
        	   	        }
        	   	    },
                   fields:['id', 'cn_name'], 
                   autoLoad:true
               	}), 
               	loadingText:'正在加载数据，请稍侯……', 
               	triggerAction:'all', 
               	valueField:'id', 
               	displayField:'cn_name',
               	listeners: {
            		'select': function(combo, records, opts) {	
    	   	    		var params = { company_id : Ext.getCmp('company_combox_id').getValue() };
    	   	    		Ext.apply(studentComboBox.getStore().proxy.extraParams, params) ;
    	   	    		//studentComboBox.getStore().load();
    	   	    		studentComboBox.enable() ;
            		}
            	}
        	},studentComboBox,recordStateComboBox,{
        		xtype:'button',
        		text:'查询',
        		iconCls:'search', 
        		handler:function(){
	        		var sid = Ext.getCmp('company_combox_id').getValue() ;
	    			if(sid!=null){
	    				var params = {company_id:Ext.getCmp('company_combox_id').getValue(), student_id:Ext.getCmp('student_combobox_id').getValue(),
	    						state:Ext.getCmp('record_state_combobox_id').getValue()};
	        		    Ext.apply(store.proxy.extraParams, params);
	        		    store.currentPage=1;
	        		    store.load();
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择校区!');
	    			}
        		}
        	},{
        		xtype:'button',
        		text:'添加',
        		iconCls:'add', 
        		handler:function(){
        			form.getForm().reset();
        			win.show();
        		}
        	},{
        		xtype:'button',
        		text:'修改',
        		iconCls:'edit', 
        		handler:function(){
	        		var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				var cid="";// 课时ID
		    			Ext.each(selected, function (item) {
		    				cid+=item.data.id;
		    			});
	    				
	    				Ext.Ajax.request({
		    			    url:'/resourcecontrol/course_real_record_get/',
		    			    method : 'post',
		    			    params:{id:cid},
		    			    success:function(response){
		    			        var record =  Ext.decode(response.responseText);
		    			        if(record[0].state!=2){// 未审核
		    			        	form.getForm().reset();
		    	        			var selected = grid.getSelectionModel().getSelection();
		    						form.loadRecord(selected[0]);
		    	        			win.show();
		    			        }else{
		    			        	Ext.Msg.alert('系统提示', '当前课时已审核,您无权对当前课时进行修改!');
		    			        }
		    			    }
		    			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要修改的课时!');
	    			}
        		}
        	},{
        		xtype:'button',
        		text:'删除',
        		iconCls:'delete', 
        		handler:function(){
	        		var selected = grid.getSelectionModel().getSelection();
	    			if(selected!=''){
	    				var cid="";// 课时ID
		    			Ext.each(selected, function (item) {
		    				cid+=item.data.id;
		    			});
	    				
	    				Ext.Ajax.request({
		    			    url:'/resourcecontrol/course_real_record_get/',
		    			    method : 'post',
		    			    params:{id:cid},
		    			    success:function(response){
		    			        var record =  Ext.decode(response.responseText);
		    			        if(record[0].state!=2){// 未审核
		    			        	Ext.MessageBox.confirm('提示', '确认删除吗?', function(btn){
				        				if(btn=='yes'){
						        			var selected = grid.getSelectionModel().getSelection();
						        			var ids = [];
						        			Ext.each(selected, function (item) {
						        				ids.push(item.data.id);
						        			});
						        			Ext.Ajax.request({
						        			    url:'/resourcecontrol/course_real_record_del/',
						        			    method : 'post',
						        			    params:{
						        			    	ids:ids.join(",")
						        			    },
						        			    success:function(response){
						        			        var results =  Ext.decode(response.responseText);
						        			        if(results.success){
						        			        	store.load();
						        			        }else{
						        			        	Ext.Msg.alert('系统提示', results.message);
						        			        }
						        			    }
						        			});
				        				}
				    				});
		    			        }else{
		    			        	Ext.Msg.alert('系统提示', '当前课时已审核,您无权对当前课时进行删除!');
		    			        }
		    			    }
		    			});
	    			}else{
	    				Ext.Msg.alert('系统提示', '请选择需要删除的课时!');
	    			}
        		}
        	}]
        }]
    });	
	   
    
    
    var form = Ext.create('Ext.form.Panel', {
		id:'form',
		width: 850,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
			xtype:'hiddenfield',
			fieldLabel:'id',
			name:'id'
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'hiddenfield',
			items:[ {
				fieldLabel : '记录时间',
				allowBlank : false,
				xtype:'datefield',
				format: 'Y-m-d',
				name : 'create_time', 
				editable:false,
				allowBlank : false,
				value : new Date(),
				blankText : '请输入'
			},courseComboBox,{
				xtype:'combobox',
				editable:false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '授课形式',
				emptyText: '请选择授课形式',
				width: 245,
				labelWidth:120,
				name : 'study_type',
				allowBlank : false,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'一对一'},
						{'id':'2','value':'一对二'},
						{'id':'3','value':'一对三'},
						{'id':'4','value':'一对四'},
						{'id':'5','value':'户外'}
					]
				}),
				columnWidth: 1/2
			}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ 
			     companyFormComboBox,
			     studentFormComboBox,
			     gradeComboBox]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ 
			{
				fieldLabel:'开始时间',
				xtype: 'timefield',
            	id: 'begin_time',
            	name: 'begin_time',
            	//width: 60,
            	minValue: '08:00',
            	maxValue: '20:59',
            	editable:false,
            	allowBlank : false,
            	value:'08:00',
            	format:'H:i',
            	increment: 15,
            	anchor: '100%'
        	},{
        		fieldLabel:'结束时间',
				xtype: 'timefield',
            	id: 'end_time',
            	name: 'end_time',
            	//width: 60,
            	minValue: '08:00',
            	maxValue: '20:59',
            	editable:false,
            	allowBlank : false,
            	value:'08:00',
            	format:'H:i',
            	increment: 15,
            	anchor: '100%'
        	},{
        		xtype:'numberfield',
        		fieldLabel : '<b><font color=red>课时数量/托管天数</font></b>',
				name : 'course_hour',
				width: 245,
				labelWidth:120,
				allowBlank : false,
				emptyText: '参与课时/托管提成',
				blankText : '请输入'
        		
        	}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[ 
			studentManagerComboBox,teacherComboBox,{
        		xtype:'combobox',
				editable:false,
				allowBlank : false,
				valueField:'id',
				displayField:'value',
				queryMode:'local',
				fieldLabel : '出席情况',
				width: 245,
				labelWidth:120,
				name : 'attend',
				value : '4',
				allowBlank : false,
				blankText : '请输入',
				store:Ext.create('Ext.data.Store',{
					fields:['id','value'], 
					data:[
						{'id':'1','value':'迟到'},
						{'id':'2','value':'请假'},
						{'id':'3','value':'旷课'},
						{'id':'4','value':'正常'}
					]
				}),
				columnWidth: 1/2
        	}]
		},{
			xtype:'container',
			layout:'hbox',
			defaultType:'textfield',
			items:[szjyComboBox]
		}]
		
	});

	
    var win = Ext.create('Ext.window.Window', {
		title : '课时记录',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [ {
			text : '保存',
			handler : function() {
				if (form.getForm().isValid()) {
					form.getForm().submit({
						waitTitle : '请稍候',
						waitMsg : '正在处理......',
						url : '/resourcecontrol/course_real_record_save/',
						success : function(form, action) {
							win.hide();
							store.load();
						},
						failure : function(form, action) {
							if (action.result) {
								Ext.Msg.alert('系统提示', action.result.message);
							} 
						}
					});
				}
			}
		}, {
			text : '取消',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});
    
    
	
    Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'courserecord-tab',
		items:[grid]
	});
	
});